package com.topway.dsmflex.ui.composite.servers;

import java.beans.PropertyChangeEvent;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

import com.Application;
import com.swtdesigner.SWTResourceManager;
import com.topway.dsmflex.Alert;
import com.topway.dsmflex.Log;
import com.topway.dsmflex.MessagePopupShell;
import com.topway.dsmflex.model.base.DataMonitorBean;
import com.topway.dsmflex.model.servers.DataServerBean;
import com.topway.dsmflex.services.servers.ServersMonitorServiescs;
import com.topway.dsmflex.thread.job.SynchroMonitor;
import com.topway.dsmflex.ui.InitFormComposite;
import com.topway.dsmflex.ui.contentProvider.TableContentProvider;
import com.topway.dsmflex.ui.labelProvider.ServersMonitorProvider;
import com.topway.dsmflex.util.StringUtil;



/**
 * 服务器监控
 * 
 * @author liusy 2013-05-8
 *
 */
public class ServersMonitor extends InitFormComposite {
	
   public static String name = "服务器监控";
   
   @SuppressWarnings("unused")
private static Logger log = Logger.getLogger(ServersMonitor.class);
   
   private Shell shell;
   private Table         tblTab;
   private CheckboxTableViewer   tvTab;
   private TableColumn   sourceTable;
   private TableColumn   colOperator;
   private TableColumn colSourceOperator;
   
   private DataServerBean  servserBean;
   private SynchroMonitor synchroMonitor = null;
   private ServersMonitorServiescs serversMonitorServiescs = null;

   
   
   
   public List<DataServerBean> servserBeanList=null;
   
   public ServersMonitor(Composite parent, int style) {
      super(parent, style);
      createContents();
      initForm();
   }
   
	   
	   
   public int initForm() {
	   this.synchroMonitor = SynchroMonitor.getSynchroMonitor();
	   this.serversMonitorServiescs = ServersMonitorServiescs.getServersMonitorServiescs();
		servserBeanList = serversMonitorServiescs.getDataServerBeanList();
		if(null !=servserBeanList && servserBeanList.size()>0)
			tvTab.setInput(servserBeanList);
		for (int i = 0; i < servserBeanList.size(); i++) {
				servserBeanList.get(i).addPropertyChangeListener(this);
			if (servserBeanList.get(i).getCheck()== 1)
			{
				tvTab.setChecked(servserBeanList.get(i), true);
			}
		}
		
	   return 1;
   }

   protected void createContents() {
	   
	  shell = this.getShell();
                                                setLayout(new FormLayout());
                                                Button btnSvae = new Button(this, SWT.NONE);
                                                FormData fd_btnSvae = new FormData();
                                                fd_btnSvae.bottom = new FormAttachment(100,-5);
                                                fd_btnSvae.left = new FormAttachment(50,-30);
                                                btnSvae.setSize(30, 50);
                                                btnSvae.setLayoutData(fd_btnSvae);
                                                //   btnSvae.setAlignment(SWT.LEFT);
                                                   btnSvae.addSelectionListener(new SelectionAdapter() {
                                                      public void widgetSelected(final SelectionEvent e) {
                                                     	 Object [] obj = (Object[]) tvTab.getCheckedElements();
                                                     	 List<DataServerBean> list = new ArrayList<DataServerBean>();
                                                     	 for (int i = 0; i < obj.length; i++) {
																list.add((DataServerBean) obj[i]);
															}
                                                     	 serversMonitorServiescs.saveTableConfigList(list);
                                                     	 if(list.size()>0)
                                                     	 {
                                                     	 	Log.clearLog();
                                                     	 	Alert.MsgBox("提示", "保存成功");
                                                     	 	//Application.openMenu(SystemLog.name);
                                                     	 }
                                                      }
                                                   });
                                                   
                                                   btnSvae.setImage(SWTResourceManager.getImage(Application.class, "/img/savesetting.gif"));
                                                   btnSvae.setFont(SWTResourceManager.getFont("", 10, SWT.BOLD));
                                                   btnSvae.setText("保存");
                                                         
                                                         
                                                         tvTab = CheckboxTableViewer.newCheckList(this, SWT.BORDER | SWT.FULL_SELECTION);
                                                         tvTab.setContentProvider(new TableContentProvider());
                                                         tvTab.setLabelProvider(new ServersMonitorProvider());
                                                         tblTab = tvTab.getTable();
                                                         FormData fd_tblTab = new FormData();
                                                         fd_tblTab.bottom = new FormAttachment(93, 0);
                                                         fd_tblTab.top = new FormAttachment(0,0);
                                                         fd_tblTab.left = new FormAttachment(0,0);
                                                         fd_tblTab.right = new FormAttachment(100,0);
                                                         tblTab.setLayoutData(fd_tblTab);
                                                         tblTab.setLinesVisible(true);
                                                         tblTab.setHeaderVisible(true);

                                                         TableColumn   colTabCheck = new TableColumn(tblTab, SWT.CHECK);
                                                         colTabCheck.setWidth(23);
                                                         
                                                         
                                                         TableColumn state = new TableColumn(tblTab, SWT.NONE);
                                                         state.setWidth(15);
                                                         state.setAlignment(SWT.RIGHT);
                                                         state.setText("  ");
                                                         
                                                         TableColumn name = new TableColumn(tblTab, SWT.NONE);
                                                         name.setWidth(150);
                                                         state.setAlignment(SWT.CENTER);
                                                         name.setText("名称");
                                                         
                                                         TableColumn statu = new TableColumn(tblTab, SWT.NONE);
                                                         statu.setWidth(50);
                                                         statu.setAlignment(SWT.CENTER);
                                                         statu.setText("激活");
                                                         
                                                         TableColumn sourceSource = new TableColumn(tblTab, SWT.NONE);
                                                         sourceSource.setWidth(250);
                                                         sourceSource.setAlignment(SWT.CENTER);
                                                         sourceSource.setText("地址");
                                                         
                                                         
                                                         colOperator = new TableColumn(tblTab, SWT.NONE);
                                                         colOperator.setWidth(300);
                                                         colOperator.setText("描述");
                                                         
                                                                    // 添加托盘右键菜单
                                                               final Menu menu = new Menu(shell, SWT.POP_UP);
                                                               final MenuItem menuAdd = new MenuItem(menu, SWT.PUSH);//开始 
                                                               menuAdd.setText("添加");
                                                               menuAdd.addSelectionListener(new SelectionAdapter() {
		@Override
		public void widgetSelected(SelectionEvent arg0) {
			AddServersMonitor adservserBeanSynchro = new AddServersMonitor(shell.getShell(), SWT.NONE, null);
			servserBean = (DataServerBean) adservserBeanSynchro.open();
			if(null==servserBean)
				return;
			tvTab.add(servserBean);
			tvTab.setChecked(servserBean, true);
		}
	});
                                                               final MenuItem menuUpdate = new MenuItem(menu, SWT.PUSH);//开始 
                                                               menuUpdate.setText("修改");
                                                               menuUpdate.addSelectionListener(new SelectionAdapter() {
		@Override
		public void widgetSelected(SelectionEvent arg0) {
 			IStructuredSelection obj = (IStructuredSelection)tvTab.getSelection();
			   servserBean = (DataServerBean) obj.getFirstElement();
			   if(null==servserBean)
			   return;
			   AddServersMonitor adservserBeanSynchro = new AddServersMonitor(shell.getShell(), SWT.NONE, servserBean);
			   DataServerBean _servserBean = (DataServerBean) adservserBeanSynchro.open();
			   if(null!=_servserBean)
			   {
				   tvTab.refresh();
			   }
		
		}
	});
                                                               final MenuItem menuDelete = new MenuItem(menu, SWT.PUSH);
                                                               menuDelete.setText("删除");
                                                               menuDelete.addSelectionListener(new SelectionAdapter() {
		@Override
		public void widgetSelected(SelectionEvent arg0) {
 			IStructuredSelection obj = (IStructuredSelection)tvTab.getSelection();
			   servserBean = (DataServerBean) obj.getFirstElement();
			   if(null==servserBean)
				   return;
			   tvTab.remove(servserBean);
		}
	});
                                                               tblTab.setMenu(menu);
            
            tvTab.addDoubleClickListener(new IDoubleClickListener() {
		@Override
		public void doubleClick(DoubleClickEvent arg0) {
			IStructuredSelection obj = (IStructuredSelection)arg0.getSelection();
			   servserBean = (DataServerBean) obj.getFirstElement();
				AddServersMonitor adservserBeanSynchro = new AddServersMonitor(shell.getShell(), SWT.NONE, servserBean);
				   DataServerBean _servserBean = (DataServerBean) adservserBeanSynchro.open();
				   if(null!=_servserBean)
				   {
					   tvTab.remove(servserBean);
					   servserBean=_servserBean;
					   tvTab.add(servserBean);
					   tvTab.setChecked(servserBean, true);
				   }
		}
	});
   }
		   
			@Override
			public void propertyChange(PropertyChangeEvent event) {
				if(DataServerBean.SERVER_STATE.equals(event.getPropertyName()))
				{
					((DataServerBean)event.getSource()).setServerState(Integer.parseInt(event.getNewValue().toString()));	
					tvTab.refresh();
					if(event.getNewValue().toString().equals("2"))
					new MessagePopupShell(Display.getDefault(),"/img/prompt.png","紧急通知","\n\n   服务器  "+((DataServerBean)event.getSource()).getName()+" 异常！                                       \n\n                 "+StringUtil.dateToString(new Date()),"详细内容");
				}
				if(DataServerBean.STATE.equals(event.getPropertyName()))
				{
					if(event.getNewValue().toString().equals("3")||event.getNewValue().toString().equals("4"))
						((DataServerBean)event.getSource()).setServerState(0);	
				}
			}
			public static void main(String[] args) {
			     
			}
}
